package edu.flying.panda.taskmanager;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

/**
 * Sort Task Screen
 * @author FlyingPanda
 */

public class SortScreen extends Activity{

	private TextView tvDisplayTime;
	private TextView tvDisplayDate;
	private Button ChangeTime;
	private Button ChangeDate;
	private Date due;
	
	
	private int hour;
	private int minute;
	private int day;
	private int month;
	private int year;
	
	static final int TIME_DIALOG_ID = 999;
	static final int DATE_DIALOG_ID = 0;

		public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sort_screen);
		
		due = new Date();
		
		final Button select_button = (Button) findViewById(R.id.sort_task_select);
		
		Bundle extras = getIntent().getExtras();
		String filter = extras.getString("filter");
		
		addListenerOnButton();
		setCurrentTaskOnView();
	
		if (filter.equals("personal")) {
			((RadioButton) findViewById(R.id.sort_screen_personal_button)).setChecked(true);
			((RadioButton) findViewById(R.id.sort_screen_school_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_work_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_completion_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_all_button)).setChecked(false);
		} else if (filter.equals("work")) {
			((RadioButton) findViewById(R.id.sort_screen_personal_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_school_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_work_button)).setChecked(true);
			((RadioButton) findViewById(R.id.sort_screen_completion_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_all_button)).setChecked(false);
		} else if (filter.equals("school")) {
			((RadioButton) findViewById(R.id.sort_screen_personal_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_school_button)).setChecked(true);
			((RadioButton) findViewById(R.id.sort_screen_work_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_completion_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_all_button)).setChecked(false);
		} else if (filter.equals("completion")) {
			((RadioButton) findViewById(R.id.sort_screen_personal_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_school_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_work_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_completion_button)).setChecked(true);
			((RadioButton) findViewById(R.id.sort_screen_all_button)).setChecked(false);
		} else {
			((RadioButton) findViewById(R.id.sort_screen_personal_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_school_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_work_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_completion_button)).setChecked(false);
			((RadioButton) findViewById(R.id.sort_screen_all_button)).setChecked(true);
		}
		
		
		
		
		
		/**
		 * Listener for Select button, cancels Task creation and returns to Home Screen
		 */
		select_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String filter = "all";
				if( ((RadioButton) findViewById(R.id.sort_screen_completion_button)).isChecked() ){
					filter = "completion";
				}
				else if( ((RadioButton) findViewById(R.id.sort_screen_personal_button)).isChecked() ){
					filter = "personal";
				}
				else if( ((RadioButton) findViewById(R.id.sort_screen_school_button)).isChecked() ){
					filter = "school";
				}
				else if( ((RadioButton) findViewById(R.id.sort_screen_work_button)).isChecked() ){
					filter = "work";
				}
				else if( ((RadioButton) findViewById(R.id.sort_screen_before_button)).isChecked() ){
					filter = "before";
				}
				else if( ((RadioButton) findViewById(R.id.sort_screen_after_button)).isChecked() ){
					filter = "after";
				}
				
				Intent myIntent = new Intent(v.getContext(), HomeScreen.class);
				myIntent.putExtra("filter", filter);
				myIntent.putExtra("date", due.toString() );
				startActivity(myIntent);}
			});}

		// display current time
				public void setCurrentTaskOnView() {
			 
					tvDisplayTime = (TextView) findViewById(R.id.sort_screen_time_text);
					tvDisplayDate = (TextView) findViewById(R.id.sort_screen_date_text);
					
					final Calendar c = Calendar.getInstance();
					hour = c.get(Calendar.HOUR_OF_DAY);
					minute = c.get(Calendar.MINUTE);
					year = c.get(Calendar.YEAR);
					day = c.get(Calendar.DAY_OF_MONTH);
					month = c.get(Calendar.MONTH);
					
					// set current time into textview
					tvDisplayTime.setText(
			                    new StringBuilder().append(pad(hour))
			                                       .append(":").append(pad(minute)));
					
					tvDisplayDate.setText(
		                    new StringBuilder().append(pad(month+1))
		                                       .append("/").append(pad(day)).append("/").append(pad(year)));
					
					due.setHour(hour);
					due.setMinute(minute);
					due.setDay(day);
					due.setMonth(month);
					due.setYear(year);
					
			 
				}
			 
				public void addListenerOnButton() {
			 
					ChangeTime = (Button) findViewById(R.id.sort_screen_time_button);
					ChangeDate = (Button) findViewById(R.id.sort_screen_date_button);
					
					ChangeTime.setOnClickListener(new OnClickListener() {
			 
						
						@SuppressWarnings("deprecation")
						public void onClick(View v) {
			 
							showDialog(TIME_DIALOG_ID);
			 
						}
			 
					});
					
					ChangeDate.setOnClickListener(new OnClickListener() {
			 
						
						@SuppressWarnings("deprecation")
						public void onClick(View v) {
			 
							showDialog(DATE_DIALOG_ID);
			 
						}
			 
					});
			 
				}
			 
				@Override
				protected Dialog onCreateDialog(int id) {
					switch (id) {
					case TIME_DIALOG_ID:
						// set time picker as current time
						return new TimePickerDialog(this, 
			                                        timePickerListener, hour, minute,false);
					case DATE_DIALOG_ID:
						return new DatePickerDialog(this, 
								datePickerListener, year, month, day);
					}
					return null;
				}
			 
				private TimePickerDialog.OnTimeSetListener timePickerListener = 
			            new TimePickerDialog.OnTimeSetListener() {
					public void onTimeSet(TimePicker view, int selectedHour,
							int selectedMinute) {
						hour = selectedHour;
						minute = selectedMinute;
			 
						// set current time into textview
						tvDisplayTime.setText(new StringBuilder().append(pad(hour))
								.append(":").append(pad(minute)));
			 
						due.setHour(hour);
						due.setMinute(minute);
						
			 
					}
				};
			 
				private DatePickerDialog.OnDateSetListener datePickerListener =
			            new DatePickerDialog.OnDateSetListener() {

			                public void onDateSet(DatePicker view, int Myear, 
			                                      int monthOfYear, int dayOfMonth) {
			                    year = Myear;
			                    month = monthOfYear;
			                    day = dayOfMonth;

			                    tvDisplayDate.setText(new StringBuilder().append(pad(month+1))
		                                .append("/").append(pad(day)).append("/").append(pad(year)));
			                    
			                    
			        			due.setDay(day);
			        			due.setMonth(month);
			        			due.setYear(year);
			                    
			                }
			            };
				
				private static String pad(int c) {
					if (c >= 10)
					   return String.valueOf(c);
					else
					   return "0" + String.valueOf(c);
				}		
}